package com.leetcode.problems057;

import java.util.ArrayList;
import java.util.List;

/**
 * https://leetcode-cn.com/problems/insert-interval/
 * @Date 2021/12/09 14:34
 * @Author ghery
 * @Version V1.0
 */
public class Solution {

    public int[][] insert(int[][] intervals, int[] newInterval) {
        int left = newInterval[0];
        int right = newInterval[1];
        boolean placed = false;
        List<int[]> ansList = new ArrayList<int[]>();
        for (int[] interval : intervals) {
            if (interval[0] > right) {
                // 在插入区间的右侧且无交集
                if (!placed) {
                    ansList.add(new int[]{left, right});
                    placed = true;
                }
                ansList.add(interval);
            } else if (interval[1] < left) {
                // 在插入区间的左侧且无交集
                ansList.add(interval);
            } else {
                // 与插入区间有交集，计算它们的并集
                left = Math.min(left, interval[0]);
                right = Math.max(right, interval[1]);
            }
        }
        if (!placed) {
            ansList.add(new int[]{left, right});
        }
        int[][] ans = new int[ansList.size()][2];
        for (int i = 0; i < ansList.size(); ++i) {
            ans[i] = ansList.get(i);
        }
        return ans;
    }



    public static void main(String[] args) {
        int[][] nums = {{1,3},{6,9}};
        int[] newInterval = {2,5};
        int[][] res = new Solution().insert(nums,newInterval);
        System.out.print("[");
        for (int[] re : res) {
            System.out.print("[");
            for (int i = 0; i < re.length; i++) {
                System.out.print(re[i]);
                if(i != re.length - 1){
                    System.out.print(",");
                }
            }
            System.out.print("]");
        }
        System.out.println("]");
    }
}
